<?php

namespace App\Http\Controllers\Manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Article;
use App\Category;
use App\Cases;
use App\Casecate;
use Excel;
use Illuminate\Support\Facades\DB;

class BatchController extends Controller
{
    /**
     * @var int 数据表id
     */
    public $table_id = 1;

    /**
     * CaseController constructor.
     */
    public function __construct()
    {
        parent::__construct($this->table_id);

    }
    // 删除文章
    public function articledel (Request $request) {
        $callback = null;
        #判断权限
        if($this->rule['delete'] == 0){
            return [
                'code' => 101,
                'msg' => '权限不足！'
            ];
        }
        $checkboxes = $request->input('chkbox');
        if($request->input('chkbox') == null) {
            return $callback = [
                'code'=> 101,
                'msg' => "请选择删除内容"
            ];
        }
        for($i = 0; $i<sizeof($checkboxes); $i++) {
            $id = $checkboxes[$i];
            if(Article::destroy($id)) {
                $callback = [
                    'code' => 101,
                    'msg' => $callback['msg'] . '删除ID：' . $id . '成功, '
                ];
            }
            else {
                $callback .= '删除ID：' . $id . '失败';
            }
        }
        return $callback;
    }

    // 文章查询
    public function articlesearch(Request $request) {
        #判断权限
        if($this->rule['view'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $categorys = Category::all();
        $category = $request->input('category');
        $title = $request->input('table_search');
        $keyword = $request->input('keyword');
        $create_time = $request->input('create_time');
        $create_time = explode("-", $create_time);
        $start = strtotime($create_time[0]);
        $end = strtotime($create_time[1]);

        if($title == "" and $keyword == "" and $create_time == "" and $category == "") {
            return redirect()->back()->withInput()->withErrors('请输入要搜索关键字！');
        }
        else {
            $arr = null;
            if($category != "") {
                $arr[] = ["category", "=", $category];
            }
            if($title != "") {
                $arr[] = ["title", "like", "%".$title."%"];
            }
            if($keyword != "") {
                $arr[] = ["keyword", "like", "%".$keyword."%"];
            }

            if($arr) {
                $result = Article::where($arr)->whereBetween('create_time', [date('Y-m-d',$start), date('Y-m-d',$end)])->get();
            }
            else {
                $result = Article::whereBetween('create_time', [date('Y-m-d',$start), date('Y-m-d',$end)])->get();
            }

            /*$result = Article::where([
                ['title', 'like', '%'.$title.'%'],
                ['keyword', 'like', '%'.$keyword.'%'],
                ['category', '=', $category]
            ])->whereBetween('create_time', [date('Y-m-d',$start), date('Y-m-d',$end)])->get();*/

            $sear_cate = $category;
            $sear_time = $request->input('create_time');
            $sear_titl = $title;
            $sear_keyw = $keyword;

            if($result) {
                return view('manage/batch/search', compact('result', 'categorys', 'sear_cate', 'sear_time', 'sear_keyw', 'sear_titl'));
            }
        }
    }

    // 文章状态设置
    public function artitatus($id) {
        #判断权限
        if($this->rule['update'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $status = Article::whereId($id)->pluck('status');
        $status = $status[0];
        if($status == 0) {
            if(Article::whereId($id)->update(['status' => 1])) {
                return redirect('manage/article')->withSuccess('文章ID:'.$id.' 状态已启用');
            }
            else {
                return redirect()->back()->withInput()->withErrors('文章ID:'.$id.' 状态启用失败');
            }
        }
        else {
            if(Article::whereId($id)->update(['status' => 0])) {
                return redirect('manage/article')->withSuccess('文章ID:'.$id.' 状态已禁用');
            }
            else {
                return redirect()->back()->withInput()->withErrors('文章ID:'.$id.' 状态禁用失败');
            }
        }
    }
    // 文章导出
    public function articleexport(Request $request) {

        $categorys = Category::all();
        $category = $request->input('category');
        $title = $request->input('table_search');
        $keyword = $request->input('keyword');
        $create_time = $request->input('create_time');
        $create_time = explode("-", $create_time);
        $start = strtotime($create_time[0]);
        $end = strtotime($create_time[1]);

        if($title == "" and $keyword == "" and $create_time == "" and $category == "") {
            return redirect()->back()->withInput()->withErrors('请输入要搜索关键字！');
        }
        else {
            $arr = null;
            if ($category != "") {
                $arr[] = ["category", "=", $category];
            }
            if ($title != "") {
                $arr[] = ["title", "like", "%" . $title . "%"];
            }
            if ($keyword != "") {
                $arr[] = ["keyword", "like", "%" . $keyword . "%"];
            }

            if ($arr) {
                $result = Article::where($arr)->whereBetween('create_time', [date('Y-m-d', $start), date('Y-m-d', $end)])->get();
            } else {
                $result = Article::whereBetween('create_time', [date('Y-m-d', $start), date('Y-m-d', $end)])->get();
            }

            if($result->all() == null) {
                return redirect()->back()->withInput()->withErrors('请输入要搜索关键字！');
            }
            else {
                $data[] = array('ID', '标题', '分类', '关键词', '创建时间', '更新时间', '作者', '浏览次数', '状态', '内容');
                foreach ($result as $res) {
                    $data[] = array($res['attributes']['id'], $res['attributes']['title'], $res['attributes']['category'], $res['attributes']['keyword'], $res['attributes']['create_time'], $res['attributes']['update_time'], $res['attributes']['author'], $res['attributes']['view'], $res['attributes']['status'], $res['attributes']['content']);
                }

                $result = Excel::create('文章列表', function($excel) use($data) {
                    $excel->sheet('Article', function($sheet) use($data) {
                        $sheet->rows($data);
                    }) ;
                })->export('xls');

                if($result) {
                    return redirect('manage/case')->withSuccess('导出成功');
                }
                else {
                    return redirect()->back()->withInput()->withErrors('导出失败！');
                }

            }
        }
    }


        /*$data[] = array('ID', '标题', '分类', '关键词', '创建时间', '更新时间', '作者', '浏览次数', '状态', '内容');
        $id = 1;
        $data1 = Article::select('id', 'title', 'category', 'keyword', 'create_time', 'update_time', 'author', 'view', 'status', 'content')->where('id', $id)->first();
        $data[] = array($data1['attributes']['id'], $data1['attributes']['title'], $data1['attributes']['category'], $data1['attributes']['keyword'], $data1['attributes']['create_time'], $data1['attributes']['update_time'], $data1['attributes']['author'], $data1['attributes']['view'], $data1['attributes']['status'], $data1['attributes']['content']);

        dump($data);*/
}
